package com.ltkj.shop.service.impl;

import com.ltkj.common.core.utils.DateUtils;
import com.ltkj.common.core.utils.StringUtils;
import com.ltkj.common.datascope.annotation.DataScope;
import com.ltkj.common.security.utils.SecurityUtils;
import com.ltkj.shop.domain.ShopGoods;
import com.ltkj.shop.domain.ShopGoodsSpec;
import com.ltkj.shop.domain.ShopStore;
import com.ltkj.shop.domain.request.ShopGoodsParams;
import com.ltkj.shop.domain.vo.ShopGoodsSpecVo;
import com.ltkj.shop.domain.vo.ShopGoodsVo;
import com.ltkj.shop.mapper.ShopGoodsBizMapper;
import com.ltkj.shop.mapper.ShopGoodsMapper;
import com.ltkj.shop.mapper.ShopGoodsSpecMapper;
import com.ltkj.shop.mapper.ShopStoreMapper;
import com.ltkj.shop.service.IShopGoodsService;
import lombok.Synchronized;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 文创商品Service业务层处理
 * 
 * @author xupf
 * @date 2023-08-29
 */
@Service
public class ShopGoodsServiceImpl implements IShopGoodsService 
{
    @Autowired
    private ShopGoodsMapper shopGoodsMapper;
    @Autowired
    private ShopGoodsBizMapper shopGoodsBizMapper;
    @Autowired
    private ShopStoreMapper shopStoreMapper;
    @Autowired
    private ShopGoodsSpecMapper shopGoodsSpecMapper;

    /**
     * 查询文创商品
     * 
     * @param id 文创商品主键
     * @return 文创商品
     */
    @Override
    public ShopGoodsVo selectShopGoodsById(Long id)
    {
        return shopGoodsBizMapper.selectShopGoodsById(id);
    }

    /**
     * 查询文创商品列表
     * 
     * @param shopGoods 文创商品
     * @return 文创商品
     */
    @Override
    @DataScope(storeAlias = "g")
    public List<ShopGoodsVo> selectShopGoodsList(ShopGoods shopGoods)
    {
        return shopGoodsBizMapper.selectShopGoodsList(shopGoods);
    }

    /**
     * 新增文创商品
     * 
     * @param shopGoods 文创商品
     * @return 结果
     */
    @Override
    public ShopGoods insertShopGoods(ShopGoods shopGoods)
    {
        ShopStore shopStore = shopStoreMapper.selectShopStoreById(shopGoods.getStoreId());
        shopGoods.setSysStoresId(shopStore.getSysStoresId());
        shopGoods.setCreateTime(DateUtils.getNowDate());
        shopGoods.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        shopGoods.setUserId(SecurityUtils.getLoginUser().getUserid());
        shopGoods.setIsDel("0");
        if (StringUtils.isNotEmpty(shopGoods.getStatus())) {
            shopGoods.setOnOffTime(DateUtils.getNowDate());
        }
        // 转换金额 元 -> 分
        shopGoods.setPrice(shopGoods.getPrice().multiply(new BigDecimal(100)));
        shopGoods.setPriceSrc(shopGoods.getPriceSrc().multiply(new BigDecimal(100)));
        shopGoods.setCost(shopGoods.getCost().multiply(new BigDecimal(100)));
        shopGoodsMapper.insertShopGoods(shopGoods);
        return shopGoods;
    }

    /**
     * 修改文创商品
     * 
     * @param shopGoods 文创商品
     * @return 结果
     */
    @Override
    public int updateShopGoods(ShopGoods shopGoods)
    {
        shopGoods.setUpdateTime(DateUtils.getNowDate());
        shopGoods.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        // 转换金额 元 -> 分
        shopGoods.setPrice(shopGoods.getPrice().multiply(new BigDecimal(100)));
        shopGoods.setPriceSrc(shopGoods.getPriceSrc().multiply(new BigDecimal(100)));
        shopGoods.setCost(shopGoods.getCost().multiply(new BigDecimal(100)));
        return shopGoodsMapper.updateShopGoods(shopGoods);
    }

    /**
     * 上架/下架文创商品
     *
     * @param shopGoods 文创商品
     * @return 结果
     */
    @Override
    public int onOffShopGoods(ShopGoodsParams shopGoods)
    {
        Date nowDate = DateUtils.getNowDate();
        shopGoods.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        shopGoods.setOnOffTime(nowDate);
        return shopGoodsBizMapper.onOffShopGoods(shopGoods);
    }

    /**
     * 批量删除文创商品
     *  只会删除下架商品
     * 
     * @param ids 需要删除的文创商品主键
     * @return 结果
     */
    @Override
    public int deleteShopGoodsByIds(Long[] ids)
    {
        return shopGoodsBizMapper.deleteShopGoodsByIds(ids);
    }

    /**
     * 删除文创商品信息
     *  只会删除下架商品
     * 
     * @param id 文创商品主键
     * @return 结果
     */
    @Override
    public int deleteShopGoodsById(Long id)
    {
        return shopGoodsBizMapper.deleteShopGoodsById(id);
    }

    /**
     * 移动端查询文创商品列表
     *
     * @param shopGoods 文创商品
     * @return 文创商品
     */
    @Override
    public List<ShopGoodsVo> selectList(ShopGoods shopGoods) {
        // 查询上架商品
        shopGoods.setStatus("1");
        return shopGoodsBizMapper.selectList(shopGoods);
    }

    /**
     * 移动端查询文创商品
     *
     * @param id 文创商品主键
     * @return 文创商品
     */
    @Override
    public ShopGoodsVo getInfo(Long id)
    {
        ShopGoodsVo shopGoodsVo = shopGoodsBizMapper.selectShopGoodsById(id);
        computeViewNum(shopGoodsVo);
        // 查询规格信息
        ShopGoodsSpec shopGoodsSpec = new ShopGoodsSpec();
        shopGoodsSpec.setGoodsId(id);
        List<ShopGoodsSpec> shopGoodsSpecs = shopGoodsSpecMapper.selectShopGoodsSpecList(shopGoodsSpec);
        if (null != shopGoodsSpecs && !shopGoodsSpecs.isEmpty()) {
            Map<String, List<ShopGoodsSpecVo>> goodsSpecs = shopGoodsSpecs.stream().map(ShopGoodsSpecVo::fromEntity).collect(Collectors.groupingBy(ShopGoodsSpecVo::getType));
            shopGoodsVo.setGoodsSpecs(goodsSpecs);
        }
        // 查询店铺信息
        ShopStore shopStore = shopStoreMapper.selectShopStoreById(shopGoodsVo.getStoreId());
        shopGoodsVo.setShopStore(shopStore);
        return shopGoodsVo;
    }



    /**
     * 计算文创商品查看数量
     *
     * @param shopGoods 文创商品
     * @return
     */
    @Transactional
    @Synchronized
    void computeViewNum(ShopGoodsVo shopGoods) {
        Long viewNum = shopGoods.getViewNum();
        if (null == viewNum) {
            viewNum = 0L;
        }
        ++viewNum;
        shopGoodsBizMapper.updateViewNum(shopGoods.getId(), viewNum);
    }
}
